<template>
    <div class="app-container">
        <!-- 添加或修改组织 -->
        <el-form ref="form" :model="deptForm" :rules="rules"  label-width="80px">
            <el-form-item label="组织ID" prop="orgCode">
                <el-input v-model="deptForm.orgCode" placeholder="请输入组织ID" :disabled="deptForm.id === undefined ? false : true" />
            </el-form-item>
            <el-form-item label="组织名称" prop="orgName">
                <el-input v-model="deptForm.orgName" placeholder="请输入组织名称" />
            </el-form-item>
            <!--<el-form-item label="组织类型" prop="orgStatus">
                <el-input v-model="deptForm.orgStatus" placeholder="请输入组织类型" />
            </el-form-item>-->
            <el-form-item label="备注" prop="remark">
                <el-input v-model="deptForm.remark" placeholder="请输入备注" />
            </el-form-item>
            <!--<el-form-item label="父级ID" prop="parentCode">
                <el-input v-model="deptForm.parentCode" />
            </el-form-item>
            <el-form-item label="ID" prop="id">
                <el-input v-model="deptForm.id" />
            </el-form-item>-->
        </el-form>
        <div class="dialog-footer" style="float:right">
            <el-button :loading="buttonLoading" type="primary" @click="submitForm">保 存</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </div>
</template>

<script>
    import { addorganization } from '@/api/organization/organization'
    export default {
        name: "deptForm",
        props: {
            // 组织表单信息
            deptForm: Object,
        },
        data() {
            var checkCode = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('组织ID不能为空'));
                } else if (!/^\w{8}$/.test(value)) {
                    return callback(new Error('必须符合8位数规则！'))
                } else {
                    callback();
                }
            };
            return {
                // 按钮loading
                buttonLoading: false,
                // 表单校验
                rules: {
                    orgCode: [
                        { required: true, validator: checkCode, trigger: "blur" }
                    ],
                    orgName: [
                        { required: true, message: "组织名称不能为空", trigger: "blur" }
                    ]
                },
            }
        },
        methods: {
            // 给页面添加权限数据
            submitForm() {
                // console.log('pageInfo===:', JSON.stringify(this.pageInfo))
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        this.buttonLoading = true;
                        this.deptForm.orgTag = this.deptForm.orgTag === undefined ? '1' : this.deptForm.orgTag;
                        if (this.deptForm.id != null) {
                            addorganization(this.deptForm).then(res => {
                                if (res.retCode === '200') {
                                    console.log(res)
                                    this.$message.success('修改成功')
                                    // this.reloadFn(res.responseBody.parentId);
                                    this.cancel();
                                } else {
                                    this.$message.error('修改失败...')
                                }
                            }).finally(() => {
                                this.buttonLoading = false;
                            });
                        } else {
                            addorganization(this.deptForm).then(res => {
                                if (res.retCode === '200') {
                                    if (res.responseBody !== 0) {
                                        this.$message.success('添加成功')
                                        this.reloadFn(res.responseBody.parentId);
                                        this.cancel();
                                    } else {
                                        this.$message.error('数据已存在！');
                                    }
                                } else {
                                    this.$message.error('添加失败...')

                                }
                            }).finally(() => {
                                this.buttonLoading = false;
                            });
                        }
                    }
                });

            },
            // 取消按钮
            cancel() {
                // 清空
                this.$emit("cancelDialog");
            },
            reloadFn(parentId) {
                this.$emit("reloadFn", parentId);
            }
        }

    }

</script>
